package gt

import (
	"gitee.com/dark.H/gs"
	"github.com/mattn/go-runewidth"
)

func (l L) Print(m gs.Str) {

	l.Sprint(m).Print()
}

func (l L) Sprint(m gs.Str, selected ...bool) gs.Str {
	m = m.Replace("\t", "    ")
	if l.Row < 0 {
		maxRow, _ := gs.GetWindowsSize()
		l.Row = maxRow + l.Row + 1
	}
	ml := runewidth.StringWidth(m.Str())
	if ml > l.Width {

		rawm := m.Printable(true)
		if m.In("\x1b[7m") {
			if rawm.Len() <= l.Width {
				m = rawm + gs.Str(" ").Repeat(l.Width-rawm.Len())
			} else {
				m = rawm[:l.Width]
			}
			m = m.ANSISelected()
		} else {
			if rawm.Len() <= l.Width {
				m = rawm + gs.Str(" ").Repeat(l.Width-rawm.Len())
			} else {
				m = rawm[:l.Width]
			}

		}

	} else if ml < l.Width {
		m = m + gs.Str(" ").Repeat(l.Width-ml)
	}
	if selected != nil {
		m = m.Printable(true) + gs.Str(" ").Repeat(m.Len()-m.Printable(true).Len())
		if m.Len() > l.Width {
			m = m[:l.Width]
		}
		m = m.ANSISelected()
	}
	return gs.Str("").ANSICursor(l.Row, l.Col).Add(m)
}
